package com.example.com.baruffini_mele.booking4u;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import com.example.com.baruffini_mele.booking4u.authorization.DatabaseHandler;
import com.google.api.client.util.Data;
import com.google.gdata.client.ClientLoginAccountType;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.database.Cursor;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;


import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import ldap.autenticazione;


public class ShowRoomCalendarActivity extends Activity {
	private static final String TAG = "ShowRoomCalendarActivity";
	 //event list
	GEvent myGE = new GEvent();
	ArrayList<GEvent> currWeekEvents = new ArrayList();
	String calendarId;
	String calendarName;
	String firstDayOfThisWeekString;
	String firstDayOfNextWeekString;
	 LinearLayout layout;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_room_calendar_layout);
        //new task().execute();
        Log.v(TAG, "carico layout");
        layout = (LinearLayout)findViewById(R.id.linear_show_room_calendar);
        layout.setOrientation(LinearLayout.VERTICAL);
        
        
        Intent intent = getIntent(); // gets the previously created intent
        calendarId = intent.getStringExtra("calendarId");
        calendarName = intent.getStringExtra("calendarName");
        Log.v(TAG, "recuperato calendarId ="+calendarId);
        
        
     // get today and clear time of day 
        Calendar cal = Calendar.getInstance(); 
        cal.clear(Calendar.HOUR_OF_DAY); 
        cal.clear(Calendar.MINUTE); 
        cal.clear(Calendar.SECOND); 
        cal.clear(Calendar.MILLISECOND);
        // get start of this week in milliseconds 
        
   
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); 
        Date firstDayOfThisWeek = cal.getTime();
        SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy-MM-dd");
        firstDayOfThisWeekString = dateformatter.format(firstDayOfThisWeek.getTime());
        //System.out.println("firstDayOfThisWeek: " + dateformatter.format(firstDayOfThisWeek.getTime()));
        
        cal.add(Calendar.WEEK_OF_YEAR, 2); 
        Date firstDayOfNextWeek = cal.getTime();  
        firstDayOfNextWeekString = dateformatter.format(firstDayOfNextWeek.getTime());
        //System.out.println("firstDayOfNextWeek: " + dateformatter.format(firstDayOfNextWeek.getTime()));
       
        TextView title = new TextView(this);
        title.setTextColor(getResources().getColor(R.color.blue));
        title.setTextSize(getResources().getDimension(R.dimen.title_3));
        title.setText(getResources().getString(R.string.show_room_calendar_occupation)+ " " +calendarName+ " "+getResources().getString(R.string.show_room_calendar_from)+" "+firstDayOfThisWeekString+" " +getResources().getString(R.string.show_room_calendar_to)+" "+firstDayOfNextWeekString);
        layout.addView(title);
        
        
        
        new task().execute();
        
       
        
    }
    
    
    
    class task extends AsyncTask<String, String, Void>
    {
    private ProgressDialog progressDialog = new ProgressDialog(ShowRoomCalendarActivity.this);
        InputStream is = null ;
        String result = "";
        protected void onPreExecute() {
       
           progressDialog.setMessage(getResources().getString(R.string.show_room_calendar_wait));
           progressDialog.show();
           progressDialog.setOnCancelListener(new OnCancelListener() {
    	@Override
    		public void onCancel(DialogInterface arg0) {
    		task.this.cancel(true);
    	   }
    	});
         }
           @Override
    	protected Void doInBackground(String... params) {
        	   //request events to remote calendar
               currWeekEvents = myGE.postCalendarEvents(calendarId, firstDayOfThisWeekString, firstDayOfNextWeekString);
                

    			return null;

    		}
    	protected void onPostExecute(Void v) {

    		
    			this.progressDialog.dismiss();
    			//dynamic events list generator
    	        Iterator<GEvent> itr = currWeekEvents.iterator();
    	        
    	        
    	        while (itr.hasNext()) {
    	        	GEvent ge = itr.next();
    	        	Log.v(TAG,ge.getTitle());
    	        	TextView tv1 = new TextView(ShowRoomCalendarActivity.this);
    	        	TextView tv2 = new TextView(ShowRoomCalendarActivity.this);
    	        	tv1.setTextColor(getResources().getColor(R.color.red));
    	        	String tv1Content = ge.getDay()+" "+ge.getEventDate()+" ore "+ge.getStartTime()+" - "+ge.getEndTime();
    	        	String tv2Content = ge.getTitle();
    	        	
    	        	tv1.setText(tv1Content);
    	        	layout.addView(tv1);
    	        	tv2.setText(tv2Content);
    	        	layout.addView(tv2);
    	        	
    	        	
    	        	
    	        }

    		
    	}
        }
    

    
    
    
    



    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_show_room_calendar_menu, menu);
        return(super.onCreateOptionsMenu(menu));
        
    }
    
    
    
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) 
        {
        	// if help selected
            case R.id.showRC_menu_home:
            	// start Main
               startActivity(new Intent(this, MainActivity.class));
            return true;
            case R.id.showRC_menu_back:
            	//start Select Room
            	startActivity(new Intent(this, SelectRoomActivity.class));
            return true;
            
            case R.id.showRC_menu_add_event:
            	// start add event
            	Intent intent = new Intent(getBaseContext(), BookingFormActivity.class);
            	intent.putExtra("calendarId",calendarId);
            	intent.putExtra("calendarName",calendarName);
            	startActivity(intent);
            return true;	

            
        }// end switch on menu item
        return(super.onOptionsItemSelected(item));
    }  
}
